package unit6.replaceMethodWithMethodObject;

import lombok.Data;

/**
 * 以函数对象取代函数
 */
public class Example1 {

    /**
     * before refactoring
     */
    int test1(int inputVal, int quantity, int yearToDate) {
        int importantValue1 = inputVal * quantity + delta();
        int importantValue2 = inputVal * yearToDate + 100;
        if (yearToDate - importantValue1 > 100) {
            importantValue2 -= 20;
        }
        int importantValue3 = importantValue2 * 7;
        return importantValue3 - 2 * importantValue1;
    }

    int delta() {
        return 0;
    }

    /**
     * after refactoring
     */
    int test1a(int inputVal, int quantity, int yearToDate) {
        return new Gamma(this, inputVal, quantity, yearToDate).compute();
    }

}

@Data
class Gamma {
    private final Example1 example1;
    private int inputVal;
    private int quantity;
    private int yearToDate;
    private int importantValue1;
    private int importantValue2;
    private int importantValue3;

    public Gamma(Example1 example1, int inputVal, int quantity, int yearToDate) {
        this.example1 = example1;
        this.inputVal = inputVal;
        this.quantity = quantity;
        this.yearToDate = yearToDate;
    }

    // 现在可以对这个方法进行 extract method 了
    int compute() {
        int importantValue1 = inputVal * quantity + example1.delta();
        int importantValue2 = inputVal * yearToDate + 100;
        if (yearToDate - importantValue1 > 100) {
            importantValue2 -= 20;
        }
        int importantValue3 = importantValue2 * 7;
        return importantValue3 - 2 * importantValue1;
    }

}
